import Vue from 'vue';
import { parseToObject } from '@/utils';
import { INITIAL_FORM_QUERY_VALUE, EXCEPTION_TYPES } from '../../constants';
import { ChannelTree } from '@/components';
import { loadStatistics } from '../../api';

Vue.component(ChannelTree.name);

export default {
  name: 'audit-price-exception-header-form',
  components: { ChannelTree },
  props: {
    topChannelList: {
      type: Array,
      default: () => [],
    },
    // 表单默认值
    defaultFormQueryValueStr: String,
    querying: {
      type: Boolean,
      default: false,
    },
  },

  data() {
    return {
      formQuery: { ...INITIAL_FORM_QUERY_VALUE },
      labelWidth: '100px',
      exceptionTypes: EXCEPTION_TYPES,
      okBtnLoading: false,
      auditTime: [],
      channels: [],

      //今日调价失败
      dayFailureCount: 0,
      //今日调价异常
      dayExceptionCount: 0,
      //今日待处理
      dayInitCount: 0,
      //今日全部处理
      dayAllCount: 0,
      //今日部分处理
      dayPartCount: 0,
      //调价失败总数
      failureAllCount: 0,
      //调价异常总数
      exceptionAllCount: 0,
      //待处理总数
      initCount: 0,
      //全部处理总数
      allCount: 0,
      //部分处理总数
      partCount: 0,
    };
  },
  watch: {
    // 监听传入的表单默认值变化
    defaultFormQueryValueStr(newValue) {
      if (!newValue) {
        return;
      }
      this.formQuery = {
        ...this.formQuery,
        ...parseToObject(newValue),
      };
    },
  },
  methods: {
    onQuery() {
      this.$emit('onQuery', { ...this.formQuery });
      this.loadStatisticsInfo();
    },
    reset() {
      this.formQuery = { ...INITIAL_FORM_QUERY_VALUE };
      this.channels = [];
      this.auditTime = [];
    },
    timeChange(time) {
      if (time) {
        this.formQuery = { ...this.formQuery, timeRange: { startTime: this.auditTime[0], endTime: this.auditTime[1] } };
      } else {
        this.auditTime = [];
        this.formQuery = { ...this.formQuery, timeRange: null };
      }
    },
    //渠道选中
    affirmChannel(channels) {
      this.channels = channels;
      this.formQuery.channelCodes = this.channels.map(item => item.id);
      this.checkedSize = channels.length;
      console.log(this.formQuery);
    },

    async loadStatisticsInfo() {
      return await loadStatistics().then(data => {
        //alert(JSON.stringify(data));
        this.dayFailureCount = data.dayFailureCount;
        this.dayExceptionCount = data.dayExceptionCount;
        this.dayInitCount = data.dayInitCount;
        this.dayAllCount = data.dayAllCount;
        this.dayPartCount = data.dayPartCount;
        this.failureAllCount = data.failureAllCount;
        this.exceptionAllCount = data.exceptionAllCount;
        this.initCount = data.initCount;
        this.allCount = data.allCount;
        this.partCount = data.partCount;
      });
    },
  },
  mounted() {
    this.loadStatisticsInfo();
  },
};
